﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;



namespace WordMine
{
    class BattleScene : Scene
    {
        private ContentManager loadcontent;
        //Background Positions
        private const int BACKGROUND_OFFSET_X = 400;
        private const int BACKGROUND_OFFSET_Y = 240;
        public List<Unit> player1Units;
        public List<Unit> player2Units;
        //Background GameObjects
        private GameObject background;
        private int maxTimer;
        //Game Data
        private Random rand;
        public Base player1Base;
        public Base player2Base;


        public BattleScene()
        {
            background = new GameObject("background/forest", new Vector2(512, 60));
            background.zindex = .01f;
            player1Base = new Base(new Vector2(20, 75));
            player2Base = new Base(new Vector2(1004, 75));
            player1Units = new List<Unit>();
            player2Units = new List<Unit>();
        }

        public override void Stop()
        {
            //Background GameObjects
            background = null;
        }

        public override void Start()
        {
            lose = false;

            //Game Data Initialization
            rand = new Random();
            player1Units = new List<Unit>();
            player2Units = new List<Unit>();
            //Background Initialization


            //Play Area Initialization
        }

        public override void LoadContent(ContentManager content)
        {

            loadcontent = content;
            player1Base.LoadContent(loadcontent);
            player2Base.LoadContent(loadcontent);
            background.LoadContent(loadcontent);
            //base.LoadContent(content);
        }
        public void checkForDeaths()
        {
            if (player1Units.Count > 0)
            {
                if (player1Units[0].hp <= 0)
                    player1Units.RemoveAt(0);
            }
            if (player2Units.Count > 0)
            {
                if (player2Units[0].hp <= 0)
                    player2Units.RemoveAt(0);
            }
        }
        public override void Update(GameTime gameTime)
        {
            if (player1Units.Count > 0)
            {
                player2Base.checkEnemy(player1Units[0]);
                for (int x = 0; x < player1Units.Count; x++)
                {
                    if (x != 0)
                        player1Units[x].checkFriendly(player1Units[x - 1]);
                    else if (player2Units.Count > 0)
                        player1Units[x].checkEnemy(player2Units[0]);
                    player1Units[x].Update(gameTime);
                }

            }
            checkForDeaths();
            if (player2Units.Count > 0)
            {
                player1Base.checkEnemy(player2Units[0]);
                for (int x = 0; x < player2Units.Count; x++)
                {
                    if (x != 0)
                        player2Units[x].checkFriendly(player2Units[x - 1]);
                    else if (player1Units.Count > 0)
                        player2Units[x].checkEnemy(player1Units[0]);
                    player2Units[x].Update(gameTime);
                }
            }
            checkForDeaths();
            //base.Update(gameTime);
        }
        public Boolean checkForEndOfRound()
        {
            if (player1Units.Count == 0 && player2Units.Count == 0)
                return true;
            return false;
        }
        public void generatePlayer1Unit(Unit unit)
        {
            if (unit != null)
            {
                unit.LoadContent(loadcontent);
                player1Units.Add(unit);
            }
        }
        public void generatePlayer2Unit(Unit unit)
        {
            if (unit != null)
            {
                unit.LoadContent(loadcontent);
                player2Units.Add(unit);
            }
        }


        public override void Draw(SpriteBatch spriteBatch)
        {
            if (player1Units.Count > 0)
            {
                foreach (Unit unit in player1Units)
                    unit.Draw(spriteBatch);
            }
            if (player2Units.Count > 0)
            {
                foreach (Unit unit in player2Units)
                    unit.Draw(spriteBatch);
            }
            
            player1Base.Draw(spriteBatch);
            player2Base.Draw(spriteBatch);
            background.Draw(spriteBatch);
            
        }
    }
}